package com.anrongtec.zcpt.service;

import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.graphics.drawable.Drawable;
import android.os.IBinder;
import android.os.RemoteException;
import android.text.TextUtils;
import android.util.Log;

import com.anrongtec.zcpt.module.AppPushMsgInfo;
import com.anrongtec.zcpt.util.NotifyManager;
import com.ansec.mdm.IPushListener;
import com.ansec.mdm.IPushService;
import com.art.base.util.ApkUtils;
import com.art.base.util.JSONHelper;
import com.art.base.util.ToastShow;
import com.art.base.util.log.LogUtil;

/**
 * 消息推送服务，绑定MDM消息推送，获取推送给终端的消息
 *
 * @author dongtianhao
 */
public class GetPushInfoService extends Service {
    private IPushService pushService;
    private static final String TAG = "GetPushInfoService";

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        boolean b = bindPushService();
        LogUtil.trace(Log.ERROR, "anrongtec", b + " test");
    }

    /**
     * 绑定推送服务
     */
    private boolean bindPushService() {
        Intent intent = new Intent("com.ansec.mdm.action.BindPush");
        intent.setPackage("com.ansec.mdm");
        LogUtil.trace(Log.ERROR, "anrongtec", " test");
        return getApplication().bindService(intent, pushConn, Context.BIND_AUTO_CREATE);
    }

    @Override
    public void onDestroy() {
        unbindService(pushConn);
        super.onDestroy();
    }

    /**
     * 推送conn，绑定MDM的定位服务
     */
    private ServiceConnection pushConn = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            LogUtil.trace(Log.DEBUG, "anrongtec", name.getClassName() + "  onServiceConnected test");
            pushService = IPushService.Stub.asInterface(service);
            try {
                pushService.registerPushListener(msgPushListener);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            LogUtil.trace(Log.DEBUG, "anrongtec", name.getClassName() + "  onServiceDisconnected test");
            try {
                pushService.unregisterPushListener(msgPushListener);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
            pushService = null;
            bindPushService();
        }
    };

    /***
     * 消息处理在这
     */
    IPushListener msgPushListener = new IPushListener.Stub() {

        @Override
        public void onReceiver(String message) throws RemoteException {
            ToastShow.shortMessage(getApplicationContext(),"收到消息");
            LogUtil.trace(Log.DEBUG, "anrongtec-", "message + test");
            if (!TextUtils.isEmpty(message)) {
                // 具体消息处理。
                LogUtil.trace(Log.DEBUG, "anrongtec-", message + "  test");
                String data = JSONHelper.getStringByKeyFromJson("data", message);
                AppPushMsgInfo info = JSONHelper.parseInfoFromJson(data, AppPushMsgInfo.class);
                if (info == null) {
                    LogUtil.trace(Log.ERROR, TAG, "apppush info is null");
                    return;
                }
                if (TextUtils.isEmpty(info.getPkgName())) {
                    LogUtil.trace(Log.ERROR, TAG, "app pkgName is null");
                    return;
                }
                switch (info.getType()) {
                    // 单纯只打开APP，这个打开无论应用是否在前台都会重新打开APP
                    case AppPushMsgInfo.TYPE_OPEN_APP:
                        Drawable drawable = ApkUtils.getAppIcon(getApplicationContext(), info.getPkgName());
                        NotifyManager.getInstance(getApplicationContext()).noticOpenAppMsg(info.getPkgName(),
                                info.getTicker(), info.getTitle(), info.getMsg(), info.getAppData(), drawable);
                        break;
                    // 携带业务数据处理
                    case AppPushMsgInfo.TYPE_HANDLE_BUS:
                        Drawable d = ApkUtils.getAppIcon(getApplicationContext(), info.getPkgName());
                        NotifyManager.getInstance(getApplicationContext()).noticAppHandleMsg(info.getPkgName(),
                                info.getUri(), info.getFlag(), info.getTicker(), info.getTitle(), info.getMsg(),
                                info.getAppData(), d);
                        break;
                }
            }
        }

    };
}
